package controller

import (
	"github.com/gin-gonic/gin"
	"k8s-dash/dto"
	"k8s-dash/middleware"
	"k8s-dash/service"
	"k8s-dash/utils"
	"k8s-dash/utils/logger"
)

type deployController struct{}

var DeployController deployController

func (p *deployController) Register(ginRouter *gin.RouterGroup) {
	nd := &deployController{}
	ginRouter.GET("/k8s/deployment/list", nd.GetDeployList)
	ginRouter.POST("/k8s/deployment/create", nd.CreateDeploy)
	ginRouter.GET("/k8s/deployment/detail", nd.GetDeployDetail)
	ginRouter.DELETE("/k8s/deployment/del", nd.DeleteDeploy)
	ginRouter.PATCH("/k8s/deployment/update", nd.UpdateDeploy)
	ginRouter.GET("/k8s/deployment/numnp", nd.GetDeployToNS)
	ginRouter.PUT("/k8s/deployment/restart", nd.RestartDeploy)
	ginRouter.GET("/k8s/deployment/scale", nd.ScaleDeploy)

}

// CreateDeploy 创建deploy
// ListPage godoc
// @Summary      创建deploy
// @Description  创建deploy
// @Tags         deploy
// @ID           /api/k8s/deploy/create
// @Accept       json
// @Produce      json
// @Param        body  body dto.DeployCreateInput  true  "body"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": ""}"
// @Router       /api/k8s/deploy/create [post]
func (d *deployController) CreateDeploy(ctx *gin.Context) {
	params := &dto.DeployCreateInput{}
	if err := params.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Deployment.CreateDeploy(params)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.CreateError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.CreateError, err))
	}
	middleware.ResponseSuccess(ctx, "创建成功")
}

// GetDeployList 获取deploy列表，支持分页过滤排序
// ListPage godoc
// @Summary      获取deploy列表
// @Description  获取deploy列表
// @Tags         deploy
// @ID           /api/k8s/deploy/list
// @Accept       json
// @Produce      json
// @Param        filter_name  query  string  false  "过滤"
// @Param        namespace    query  string  false  "命名空间"
// @Param        page         query  int     false  "页码"
// @Param        limit        query  int     false  "分页限制"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": service.DeployResp}"
// @Router       /api/k8s/deploy/list [get]
func (d *deployController) GetDeployList(ctx *gin.Context) {
	parmas := &dto.DeployListInput{}
	//参数验证
	err := parmas.BindValidate(ctx)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
		return
	}
	deployList, err := service.Deployment.GetDeployList(parmas)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, deployList)
}

// GetDeployDetail 获取deploy详情
// ListPage godoc
// @Summary      获取deploy详情
// @Description  获取deploy详情
// @Tags         deploy
// @ID           /api/k8s/deploy/detail
// @Accept       json
// @Produce      json
// @Param        deploy_name  query  string  true  "deploy_name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": appsV1.Deployment}"
// @Router       /api/k8s/deploy/detail [get]
func (d *deployController) GetDeployDetail(ctx *gin.Context) {
	parmas := &dto.DeployNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	deployDetail, err := service.Deployment.GetDeployDetail(parmas)
	if err != nil {
		return
	}
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, deployDetail)
}

// DeleteDeploy 删除deploy
// ListPage godoc
// @Summary      删除deploy
// @Description  删除deploy
// @Tags         deploy
// @ID           /api/k8s/deploy/delete
// @Accept       json
// @Produce      json
// @Param        deploy_name  query  string  true  "deploy_name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/deploy/delete [delete]
func (d *deployController) DeleteDeploy(ctx *gin.Context) {
	parmas := &dto.DeployNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Deployment.DeleteDeploy(parmas.DeployName, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.DeleteError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.DeleteError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}

// UpdateDeploy 更新deploy
// ListPage godoc
// @Summary      更新deploy
// @Description  更新deploy
// @Tags         deploy
// @ID           /api/k8s/deploy/update
// @Accept       json
// @Produce      json
// @Param        content  query  string  true  "更新内容"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/deploy/update [patch]
func (d *deployController) UpdateDeploy(ctx *gin.Context) {
	parmas := &dto.DeployUpdateInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Deployment.UpdateDeploy(parmas.Content, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.UpdateError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.UpdateError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}

// GetDeployToNS 获取ns下deploy数量
// ListPage godoc
// @Summary      获取ns下deploy数量
// @Description  获取ns下deploy数量
// @Tags         deploy
// @ID           /api/k8s/deploy/deployNp
// @Accept       json
// @Produce      json
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": string}"
// @Router       /api/k8s/deploy/numnp [get]
func (p *deployController) GetDeployToNS(ctx *gin.Context) {
	deployToNS, err := service.Deployment.GetDeployToNS()
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, deployToNS)
}

// ScaleDeploy 扩缩容deploy
// ListPage godoc
// @Summary      扩缩容deploy
// @Description  扩缩容deploy
// @Tags         deploy
// @ID           /api/k8s/deploy/scale
// @Accept       json
// @Produce      json
// @Param        deploy_name  query  string  true  "deploy_name"
// @Param        namespace    query  string  true  "命名空间"
// @Param        scale_num    query  int  true  "副本数"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/deploy/scale [get]
func (d *deployController) ScaleDeploy(ctx *gin.Context) {
	parmas := &dto.DeployScaleInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	num, err := service.Deployment.ScaleDeploy(parmas.DeployName, parmas.Namespace, parmas.ScaleNum)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, num)
}

// RestartDeploy 重启deploy
// ListPage godoc
// @Summary      重启deploy
// @Description  重启deploy
// @Tags         deploy
// @ID           /api/k8s/deploy/restart
// @Accept       json
// @Produce      json
// @Param        deploy_name  query  string  true  "deploy_name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/deploy/restart [put]
func (d *deployController) RestartDeploy(ctx *gin.Context) {
	parmas := &dto.DeployNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Deployment.RestartDeploy(parmas.DeployName, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.UpdateError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.UpdateError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}
